/*********************************************************************************
 *      Copyright:  (C) 2023 Zhu Lijun<3262465970@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  lescan_init.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2023年07月24日)
 *         Author:  Zhu Lijun <3262465970@qq.com>
 *      ChangeLog:  1, Release initial version on "2023年07月24日 10时46分39秒"
 *                 
 ********************************************************************************/


#include "main.h"

#define  BLE_SCAN_TIMEOUT   8   //8s



/*扫描回调函数*/
static void ble_discovered_callback(void *adapter,const char* addr, const char* name, void* user_data)              //regular scanning
{
    if(name)
    {
        log_warn("BLE Discovered %s - '%s' ", addr, name);
    }
    else
    {
        log_warn("BLE Discovered %s - 'unknown' ", addr);
    }

    return ;
}


int  lescan_init(void *adapter, const char *adapter_name)
{
	int        temp;


	temp =  gattlib_adapter_open(adapter_name, &adapter);
	if(temp)
	{
        	log_error("Adapter open failed: %s ", strerror(errno));
        	return 1;
   	}

	temp = gattlib_adapter_scan_enable(adapter, ble_discovered_callback, BLE_SCAN_TIMEOUT, NULL);
	if (temp)
	{
		log_error("Fail to scan: %s ", strerror(errno));
        	return -1;
	}


	if (gattlib_adapter_scan_disable(adapter) != GATTLIB_SUCCESS) 
	{
        	log_error("Fail to stop scan: %s ", strerror(errno));
	        return -1;
	}
}


